#include "StdAfx.h"
#include "HeightMap.h"


HeightMap::HeightMap(void)
{
}

HeightMap::~HeightMap(void)
{
}

bool HeightMap::LoadRaw(const std::string& map)
{
	const int m_maxY = 10;

	FILE  *file;
	fopen_s(&file, map.c_str (), "rb");
	if (!file)
		return false;

	fread(&m_sizeX_, sizeof(unsigned short), 1, file);
	fread(&m_sizeZ_, sizeof(unsigned short), 1, file);

	unsigned int size = m_sizeX_ * m_sizeZ_;
	unsigned char *tmp = new unsigned char[size];
	m_height_ = new float[size];
	fread(tmp, sizeof(unsigned char), size, file);
	fclose(file);

	int i = 0;
	for (unsigned short z = 0; z < m_sizeZ_; ++z)
		for (unsigned short x = 0; x < m_sizeX_; ++x, ++i)
			m_height_[i] = float ((m_maxY * tmp[i]) / 255.0f);

	delete[] tmp;
	return true;
}